package com.closer.server.biz.images.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.closer.server.biz.images.dto.ImageVO;
import com.closer.server.biz.images.dto.SearchRequest;
import com.closer.server.biz.images.entity.Images;
import com.closer.server.biz.images.validator.ImagesValidator;
import com.closer.server.mapper.image.ImagesMapper;
import com.closer.server.support.exception.ErrorCode;
import com.closer.server.support.exception.ThrowUtils;
import com.closer.server.support.image.ImageProperties;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.closer.server.biz.images.service.ImagesService;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Paths;

/**
 * 图片资源 服务层实现。
 *
 * @author closer
 */
@Service
public class ImagesServiceImpl extends ServiceImpl<ImagesMapper, Images>  implements ImagesService{

    private final ImageProperties properties;

    private final ImagesValidator validator;

    public ImagesServiceImpl(ImageProperties properties, ImagesValidator validator){
        this.properties = properties;
        this.validator = validator;
    }


    @Override
    public long upload(String type, InputStream file) {
        // 验证图片并获取保存的文件
        var image=validator.validate(type, file);
        String extName = FileUtil.extName(image);

        //1.创建资源路径
        if (!FileUtil.exist(properties.getUploadBaseDir())){
            FileUtil.mkdir(properties.getUploadBaseDir());
        }
        //2.构建图片名称
        var currentDate = DateUtil.today();
        var rand = IdUtil.simpleUUID();
        var fileName = currentDate + "-" + rand + "." + extName;

        var IMAGE_PATH = Paths.get("image/upload/"+type, fileName).toString();
        //3.保存图片
        var saveFile = new File(Paths.get(properties.getUploadBaseDir(), IMAGE_PATH).toString());
        FileUtil.copy(image.toPath(), saveFile.toPath());

        var img = Images.builder()
                .type(type)
                .path(IMAGE_PATH)
                .enabled(false)
                .build();
        var result = this.save(img);
        ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR,"图片上传失败");
        return img.getId();
    }

    @Override
    public void enable(Long id) {
        validator.validateExitImage(id);

        // 构建更新对象
        Images updateImage = new Images();
        updateImage.setId(id);
        updateImage.setEnabled(true);

        // 执行更新
        boolean result = this.updateById(updateImage);
        ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR, "启用图片失败");
    }

    @Override
    public void disable(Long id) {
        validator.validateExitImage(id);

        // 构建更新对象
        Images updateImage = new Images();
        updateImage.setId(id);
        updateImage.setEnabled(false);

        // 执行更新
        boolean result = this.updateById(updateImage);
        ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR, "禁用图片失败");
    }

    @Override
    public void delete(Long id) {
        var img = validator.validateExitImage(id);
        var path = Paths.get(properties.getUploadBaseDir() + img.getPath());
        FileUtil.del(path);
        var result = this.removeById(id);
        ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR,"删除图片操作失败");
    }

    @Override
    public Page<ImageVO> search(SearchRequest params) {
        validator.validate(params);
        return this.mapper.getImageVOList(params);
    }
}
